<template>
  <div>
    <el-card class="box-card">
      <Cartgory :visible="visible" />
    </el-card>
    <el-card class="box-card" v-if="visible === 0">
      <div slot="header" class="clearfix">
        <el-button
          type="primary"
          icon="el-icon-plus"
          :disabled="!cartGory3Id"
          @click="visible = 1"
        >
          添加SPU
        </el-button>
      </div>
      <!-- 表格 -->
      <el-table :data="records" border style="width: 100%">
        <!-- 序号 -->
        <el-table-column
          prop="index"
          label="序号"
          width="50"
          type="index"
        ></el-table-column>
        <!-- SPU名称 -->
        <el-table-column
          prop="spuName"
          label="SPU名称"
          width="400"
        ></el-table-column>
        <!-- SPU描述 -->
        <el-table-column
          prop="description"
          label="SPU描述"
          width="400"
        ></el-table-column>

        <!-- 操作栏 -->
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="addS(row)"
            ></el-button>
            <el-button
              type="warning"
              icon="el-icon-edit"
              size="mini"
            ></el-button>
            <el-button type="info" icon="el-icon-info" size="mini"></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              slot="reference"
              @click="delSpuOne(row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[3, 6, 9, 12]"
        :page-size="limit"
        layout=" prev, pager, next, jumper,sizes,total"
        :total="total"
      ></el-pagination>
    </el-card>
    <AddSpu v-if="visible === 1" :visible.sync="visible" />
    <Updatespu v-if="visible === 2" :visible.sync="visible" />
  </div>
</template>

<script>
import Cartgory from '../../../components/cartGory'
import Updatespu from '../spu/updataspu'
import { mapState, mapMutations } from 'vuex'
import { getSpuList, delSpuSale } from '../../../api/spu'
import AddSpu from './addspu'

export default {
  name: 'Spu',

  data() {
    return {
      deleteShow: false,
      visible: 0,
      currentPage: 1,
      records: [],
      total: 0,
      limit: 3
    }
  },
  methods: {
    ...mapMutations('spu', ['SETSALEID']),
    handleSizeChange(val) {
      this.limit = val
      this.getSpu()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getSpu()
    },
    //获取spu列表
    async getSpu() {
      if (this.cartGory3Id) {
        let { currentPage, limit } = this
        let res = await getSpuList({
          page: currentPage,
          limit,
          category3Id: this.cartGory3Id
        })
        this.records = res.records
        this.total = res.total
      }
    },

    //删除spu属性
    async delSpuOne(row) {
      try {
        await delSpuSale(row.id)
        this.getSpu()
        this.$message({
          message: '删除成功',
          type: 'success'
        })
      } catch (error) {}
    },

    //跳转到添加页面并修改id
    addS(row) {
      this.visible = 2
      console.log(row.id)
      this.SETSALEID(row.id)
    }
  },
  computed: {
    ...mapState('cartgory', ['cartGory3Id'])
  },
  watch: {
    cartGory3Id: {
      handler() {
        this.getSpu()
      },
      immediate: true
    },
    visible: {
      handler(newValue) {
        if (!newValue) {
          this.getSpu()
        }
      },
      immediate: true
    }
  },

  components: {
    Cartgory,
    AddSpu,
    Updatespu
  }
}
</script>

<style scoped lang="scss">
.box-card {
  margin: 10px;
}
.el-pagination {
  text-align: center;
}
</style>
